package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpExpr;
import com.itheima.pojo.JobOption;
import com.itheima.service.EmpLogService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 员工管理
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpLogService empLogService;

    @Override
    public void savaEmpAndExprList ( Emp emp ) {
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());
        emp.setPassword("123456");
        this.save(emp);

        List<EmpExpr> exprList= emp.getExprList();
        exprList.forEach(expr->{
            expr.setEmpId(emp.getId());
        });
        empExprMapper.insert(exprList);
    }

    @Override
    public void deleteEmpAndExprList ( List<Integer> ids ) {
        this.removeByIds(ids);

        LambdaQueryWrapper<EmpExpr> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.in(EmpExpr::getEmpId,ids);
        empExprMapper.delete(queryWrapper);
    }

    @Override
    public Emp getEmpAndExprById ( Integer id ) {
        Emp emp=this.getById(id);
        LambdaQueryWrapper<EmpExpr> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EmpExpr::getEmpId,id);
        List<EmpExpr> exprList = empExprMapper.selectList(queryWrapper);
        emp.setExprList(exprList);
        return emp;
    }

    @Override
    public void updateEmpAndExprList ( Emp emp ) {
        emp.setUpdateTime(LocalDateTime.now());
        this.updateById(emp);

        LambdaQueryWrapper<EmpExpr> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(EmpExpr::getEmpId,emp.getId());
        empExprMapper.delete(queryWrapper);

        List<EmpExpr> exprList =emp.getExprList();
        exprList.forEach(expr ->{
            expr.setEmpId(emp.getId());
        });
        empExprMapper.insert(exprList);
    }

//    @Override
//    public PageResult findPage ( EmpQueryParam empQueryParam ) {
//
//        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
//        Page page1 = (Page) empMapper.selectList(null);
//        long total = page1.getTotal();
//        List<Emp> rows = page1.getResult();
//
//        return new PageResult(total, rows);
//    }
//
//    @Override
//    @RecordLogAnno
//    @Transactional(rollbackFor = Exception.class)
//    public void save ( Emp emp ) throws Exception {
//        emp.setCreateTime(LocalDateTime.now());
//        emp.setUpdateTime(LocalDateTime.now());
//        emp.setPassword("123456");
//        empMapper.insert(emp);
//
//        EmpLog empLog = new EmpLog(null, LocalDateTime.now(), emp.toString());
//        empLogService.insertLog(empLog);
//
//
//        Integer empId = emp.getId();
//        List<EmpExpr> exprList = emp.getExprList();
//        if (exprList != null && !exprList.isEmpty()) {
//            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
//            empExprMapper.insertBatch(exprList);
//        }
//    }
//
//    @Transactional
//    @Override
//    @RecordLogAnno
//    public void delete ( List<Integer> ids ) {
//        empMapper.deleteByIds(ids);
//        empExprMapper.deleteByEmpIds(ids);
//    }
//
//    @Override
//    public Emp getById ( Integer id ) {
//        Emp emp = empMapper.selectById(id);
//        List<EmpExpr> exprList = empExprMapper.getByEmpId(id);
//        emp.setExprList(exprList);
//        return emp;
//    }
//
//    @Transactional
//    @Override
//    @RecordLogAnno
//    public void update ( Emp emp ) {
//        emp.setUpdateTime(LocalDateTime.now());
//
//        empMapper.updateById(emp);
//
//
//        Integer empId = emp.getId();
//        empExprMapper.deleteByEmpIds(Arrays.asList(empId));
//
//        List<EmpExpr> exprList = emp.getExprList();
//
//        if (exprList != null && !exprList.isEmpty()) {
//            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
//            empExprMapper.insertBatch(exprList);
//        }
//    }
//
    @Override
    public JobOption getEmpJobData () {
        List<Map> list = empMapper.countEmpJobData();
        List jobList = new ArrayList<>();
        List dataList = new ArrayList<>();
        for (Map map : list) {
            jobList.add(map.get("jobName").toString());
            dataList.add(map.get("nums").toString());

        }

        return new JobOption(jobList, dataList);
    }

    @Override
    public List<Map> getEmpGenderData () {
        return empMapper.countEmpGenderData();
    }
//
//    @Override
//    public List<Emp> findAllEmp () {
//        List<Emp> empList = empMapper.selectList(null);
//        return empList;
//    }
}